package com.etao.restaurantapi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.etao.restaurantapi.service.inter.ISupplierDishAction;
import com.etao.restaurantcore.util.DESCrypt;
import com.etao.restaurantcore.util.JsonUtil;
import com.etao.restaurantentity.common.ListResponse;
import com.etao.restaurantentity.enums.SupplierServiceType;
import com.etao.restaurantentity.req.SupplierLogin;
import com.etao.restaurantentity.resp.ObjResult;
import com.etao.restaurantentity.resp.ReturnValue;
import com.etao.restaurantentity.resp.SupplierDishModel;
import com.etao.restaurantentity.resp.SupplierDishResp;
import com.etao.restaurantentity.resp.SupplierQuery;
import com.etao.restaurantentity.servicedomain.SupplierDish;
import com.etao.restaurantentity.servicedomain.SupplierServiceInfo;
import com.etao.restaurantservice.service.inter.ISupplierDishService;

@Service
public class SupplierDishAction implements ISupplierDishAction {
	private static Logger logger = LoggerFactory.getLogger("com.etao.restaurantapi");
	@Autowired
	private ISupplierDishService supplierDishService;

	public ListResponse<SupplierDishResp> selectSupplierDishMenu(int id, int supplierMenuCategoryTypeId) {
		logger.info(
				String.format("请求URL:%s 请求方式:%s 请求参数:%s", "/Api/Supplier/Menu", "Get|application/x-www-form-urlencoded",
						"?id=" + id + "&supplierMenuCategoryTypeId=" + supplierMenuCategoryTypeId));
		SupplierDish supplierDish = new SupplierDish();
		supplierDish.setSupplierID(id);
		supplierDish.setSupplierMenuCategoryTypeId(supplierMenuCategoryTypeId);

		List<SupplierDish> supplierDishs = supplierDishService.selectSupplierDishMenu(supplierDish);
		ListResponse<SupplierDishResp> resp = new ListResponse<SupplierDishResp>();

		Map<String, SupplierDishResp> resultMap = new HashMap<String, SupplierDishResp>();
		List<SupplierDishResp> list = new ArrayList<SupplierDishResp>();
		for (SupplierDish sd : supplierDishs) {
			SupplierDishResp item = new SupplierDishResp();
			item.setCategoryName(sd.getCategoryName());
			item.setCategoryId(sd.getCategoryID());
			SupplierDishModel model = new SupplierDishModel();
			model.setImagePath("http://old.etaoshi.com/" + sd.getImagePath());
			model.setSupplierDishName(sd.getSupplierDishName());
			model.setPrice(sd.getPrice());
			model.setSupplierDishId(sd.getSupplierDishID());
			model.setSuppllierDishDescription(sd.getSuppllierDishDescription());
			model.setAverageRating(sd.getAverageRating());
			model.setIsCommission(sd.getIsCommission());
			model.setIsDiscount(sd.getIsDiscount());
			model.setRecipe(sd.getRecipe());
			model.setRecommended(sd.getRecommended());
			model.setPackagingFee(sd.getPackagingFee());
			model.setSupplierCatogryId(sd.getCategoryID());
			model.setSupplierMenuCategoryId(sd.getSupplierMenuCategoryId());
			item.setSupplierDishList(new ArrayList<SupplierDishModel>());
			item.getSupplierDishList().add(model);
			if (resultMap.containsKey(sd.getCategoryName())) {
				resultMap.get(sd.getCategoryName()).getSupplierDishList().add(model);
			} else {
				resultMap.put(sd.getCategoryName(), item);
			}
		}
		for (Map.Entry<String, SupplierDishResp> entry : resultMap.entrySet()) {
			list.add(entry.getValue());
		}
		resp.setResult(list);
		return resp;
	}

	/**
	 * 商户登录
	 */
	@Override
	public ReturnValue SupplierLogin(MultivaluedMap<String, String> params) {
		String jsonUserInfo=params.getFirst("jsonUserInfo");
		logger.info(String.format("请求参数为%s", jsonUserInfo));
		ReturnValue result = new ReturnValue();

		try {
			SupplierLogin userInfo = JsonUtil.json2pojo(jsonUserInfo, SupplierLogin.class);
			String sign = DESCrypt.MD5("etsapi2012611bhl");
			if (!sign.equals(userInfo.getSign())) {
				result.setError(false);
				result.setRvNum(1);
				result.setErrarMess("签名错误");
				return result;
			}
			if (StringUtils.isEmpty(userInfo.getPwd()) || StringUtils.isEmpty(userInfo.getUserName())) {
				result.setError(false);
				result.setRvNum(1);
				result.setErrarMess("用户名/密码不能为空");
				return result;
			}
			Map<String, String> map = new HashMap<String, String>();
			map.put("userName", userInfo.getUserName());
			map.put("pwd", userInfo.getPwd());
			List<SupplierQuery> supplierQuery = supplierDishService.GetSupplierQuery(map);
			if (supplierQuery.size() != 1) {
				result.setError(false);
				result.setRvNum(1);
				result.setErrarMess("用户名/密码错误");
				return result;
			}
			result.setRvNum(0);
			boolean flag = false;
			SupplierQuery model = supplierQuery.get(0);
			if (supplierDishService.GetFeatureCount(model.getSupplierID()) > 0) {
				flag = true;
			}
			Map<String, Integer> map1 = new HashMap<>();
			map1.put("SupplierId", model.getSupplierID());
			map1.put("Type", SupplierServiceType.sms.ordinal());
			SupplierServiceInfo info = supplierDishService.FindBySupplierId(map1);
			map1 = new HashMap<>();
			map1.put("SupplierId", model.getSupplierID());
			map1.put("Type", SupplierServiceType.edm.ordinal());
			SupplierServiceInfo info2 = supplierDishService.FindBySupplierId(map1);
			ObjResult objResult = new ObjResult(model.getSupplierID(), model.getDelMinOrderAmount(),
					model.getFreeDeliveryLine(), model.getFixedDeliveryCharge(), model.getPackagingFee(),
					model.getPackLadder(), flag, model.getSupplierCustomerID(), info.getCount(), info2.getCount(),
					model.getSupplierName(), model.getAddress1(), model.getTelephone());
			result.setObjResult(objResult);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("SupplierLogin");
			result.setError(true);
			result.setRvNum(3);
			result.setErrarMess(e.getMessage());
			return result;
		}
		return result;
	}

}